<?php
define('IMGNAME', '72_,220_,440_');
define('IMGSIZE', '72,220,440');

class GoodsAction extends Action
{
    public function index() {

    	import("ORG.Util.Page");

        $Form = M('goods_base_info as g');

        $count = $Form->count();
        $Page = new Page ($count , 15 );
        $show = $Page->show();
        $list = $Form->limit( $Page->firstRow . ',' . $Page->listRows )->table('24_goods_base_info g, 24_Star s')
        		->where('g.goods_star_code=s.id')->select();
        $this->assign('SessionUame' , $_SESSION['userName']);
        $this->assign('page' , $show);
        $this->assign('list' , $list);
        $this->display();
    }

     public function goodsDel(){

        $goods_code = $_GET['goods_code'];
        $M = M('goods_base_info');
        $PicPath = $M->where("goods_code=$goods_code")->getField('goods_photo_add');
        $result = $M->where("goods_code=$goods_code")->delete();

        if( $result ){
			$imgname = explode( ',' , IMGNAME );
			foreach($imgname as $key=>$val){
				unlink("./Public/Uploads/Goods/". $val . $PicPath);
			}
            $this->assign("jumpUrl","__URL__/index");
            $this->success('删除成功！');
        }
    }

    public function goodsAdd() {

    	import("ORG.Net.UploadFile");

     	$goodsName = $_POST['goods_name'];
        $goods_code = $_POST['goods_code'];

        if( !empty($_FILES['goods_photo']['name']) ){
            $upload = new UploadFile();
            $upload->maxSize  = '3145728' ;
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
            $upload->savePath =  "./Public/Uploads/Goods/";
            $upload->imageClassPath = import("ORG.Util.Image");
            $upload->thumb = true;
			$upload->thumbRemoveOrigin = true;
            $upload->thumbPrefix = IMGNAME;
            $upload->saveRule = 'goods_'.time();
            $upload->thumbMaxWidth = IMGSIZE;
            $upload->thumbMaxHeight = '';

            if( !$upload->upload() ) {
                $this->error($upload->getErrorMsg());
            }else{
                $photoInfo = $upload->getUploadFileInfo();
            }
        }

        $M = M('goods_base_info');
        $data['goods_name'] = $_POST['goods_name'];
        $data['goods_path'] = str_replace('http://', '', $_POST['goods_path']);
        $data['goods_class'] = $_POST['goods_class'];
        $data['goods_price'] = $_POST['goods_price'];
        $data['goods_star_code'] = $_POST['goods_star_code'];
        $data['goods_class_code'] = $_POST['goods_class_code'];
		$data['goods_color'] = $_POST['goods_color'];

        if( !empty($goodsName) &&  empty($goods_code) ){
            $Form = D("goods_base_info");
            if ( $Form->create() ) {
          	    if( !empty($photoInfo) ){
                    $data['goods_photo_add'] = $photoInfo[0]['savename'];
                }
                if ( false !== $Form->add($data) ) {
					//获取明星对应标签存入关系表中
					$tagsRel = M('tags_goods_relation');
					$tagIds = trim($_POST['tagids'],',');
					$tagsArr = explode(',',$tagIds);
					$tagData['goods_id'] = $Form->getLastInsID();
					if( is_array($tagsArr) ){
						foreach($tagsArr as $key=>$val){
							$tagData['tags_id'] = $val;
							$tagsRel->add($tagData);
						}
					}
                	$this->assign("jumpUrl","index");
                    $this->success('数据添加成功！');
                } else {
                	$this->assign("jumpUrl","index");
                    $this->error('数据写入错误');
                }
            }
            exit;
        }elseif( !empty($goods_code) ){
            if ($vo = $M->create()) {
            	$data['goods_code'] = $goods_code;
                if( !empty($photoInfo) ){
                    $oldPicPath = $M->where("goods_code=$goods_code")->getField('goods_photo_add');
                    $data['goods_photo_add'] = $photoInfo[0]['savename'];
                }
                $list = $M->save($data);
            }
            if ($list !== false) {
                if( !empty($oldPicPath) ){
                    $imgname = explode( ',' , IMGNAME );
					foreach($imgname as $key=>$val){
						unlink("./Public/Uploads/Goods/". $val . $oldPicPath);
					}
                }
            	$this->assign("jumpUrl","index");
                $this->success('数据更新成功！');
            } else {
            	$this->assign("jumpUrl","index");
                $this->error("没有更新任何数据!");
            }
			exit;
        }
        $editCode = $_GET['goods_code'];
        $editInfo = $M->where("goods_code=$editCode")->select();
        $classInfo = $M->where()->select();
        $this->assign('editCode' , $editCode);
        $this->assign('editInfo' , $editInfo);


     	$Star = M("Star");
     	$starInfo = $Star->select();

     	$Class = M("class_base_info");
     	$classInfo = $Class->select();

     	$this->assign('starInfo' , $starInfo);
     	$this->assign('classInfo' , $classInfo);
        $this->display('goodsadd');
    }

    public function goodsStatus(){

    	$M = M('goods_base_info');

        $goods_status = $_GET['goods_status'];
        $goods_code = $_GET['goods_code'];

    	if ( !empty($goods_code) ) {
        	$data['goods_status'] =  $goods_status;
        	$data['goods_code'] = $goods_code;
			$data['pub_create_date'] = date("Y-m-d H:i:s" ,time() );
            $list = $M->save($data);
        }
        if ($list !== false) {
        	$this->assign("jumpUrl","__URL__/index");
            $this->success(' ');
        } else {
        	$this->assign("jumpUrl","__URL__/index");
            $this->error(' ');
        }
    }

    public function goodsRel(){

        import("ORG.Util.Page");

        $goodsCode = $_GET['goods_code'];
        $Form = M('goods_base_info');

        $count = $Form->count();
        $Page = new Page ($count , 15 );
        $show = $Page->show();
        $list = $Form->limit( $Page->firstRow . ',' . $Page->listRows )->table('goods_base_info g, star_base_info s, class_base_info c')
                ->where('g.goods_star_code=s.star_code and g.goods_type=c.class_code and goods_code!='.$goodsCode)->select();

        $goodsName = $Form->where('goods_code='.$goodsCode)->getField('goods_name');

        $this->assign('SessionUame' , $_SESSION['userName']);
        $this->assign('goodsName' , $goodsName);
        $this->assign('goodsCode' , $goodsCode);
        $this->assign('page' , $show);
        $this->assign('list' , $list);
        $this->display('relation');
    }

    public function relactionAdd(){

       $goodsCode = $_POST['goodsCode'];
       $checkCodes = trim( $_POST['checkCodes'] , ',' );
       $arrCodes = explode(',', $checkCodes);
       if( count($arrCodes) > 5 ){
            $this->assign("jumpUrl","__URL__/goodsRel/goods_code/".$goodsCode );
            $this->error('关联商品最多5件');
       }

       if( !empty($checkCodes) ){
           $Form = D('goods_relation_info');
           $data['goods_code'] = $goodsCode ;
           $data['relation_arr'] = $checkCodes;
           //if ( $Form->create() ) {
                if ( false !== $Form->add($data) ) {
                    $this->assign("jumpUrl","index" );
                    $this->success('数据添加成功！');
                } else {
                    $this->assign("jumpUrl","index" );
                    $this->error('数据写入错误');
                }
           // }
            exit;
       }
    }
	//修改商品人气数
	public function updateNum(){
		$goodsCode = $_GET['code'];
		$num = $_GET['num'];
		$Form = D('goods_base_info');
		$data['goods_code'] = $goodsCode ;
		$data['goods_like_num'] = $num;
		$res = $Form->save($data);
		if($res){
			$this->success('修改成功！');
		}else{
			$this->error($Admin->getError());
		}
	}
	//申请商品列表
	public function goodsRecom(){
		$this->display('goodsrecom');
	}

	public function getTags(){
		$starId = $_POST['id'];
		$Tags = M('tags');
		$tagsInfo = $Tags->where("star_id=$starId")->select();

		foreach($tagsInfo as $key=>$val){
			$html .= '<input type="checkbox" name="tags" value="'.$val['id'].'">&nbsp;'.$val['tags_name'].'&nbsp;&nbsp;';
		}
		
		echo $html;
	}
 }
 ?>